package a630wj.recycleview630wj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import a630wj.recycleview630wj.R;
import a630wj.recycleview630wj.iface.RegiestListener;
import a630wj.recycleview630wj.model.RegiestModel;

/**
 * Created by weijie on 2017/6/8.
 */

public class register_Activity extends AppCompatActivity implements RegiestListener,  View.OnClickListener {
    private EditText et_userName,et_password,et_rePassword,et_tel;
    private Button btn_register, btn_return;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_activity);
        initView();
    }

    private void initView() {
        et_userName=(EditText)findViewById(R.id.zusername);
        et_rePassword=(EditText)findViewById(R.id.repassword);
        et_password=(EditText)findViewById(R.id.zpassword);
        et_tel=(EditText)findViewById(R.id.ztel);
        btn_register=(Button)findViewById(R.id.zregiest);
        btn_return=(Button)findViewById(R.id.zreturn);
        btn_register.setOnClickListener(this);
        btn_return.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zregiest:
                String userName=et_userName.getText().toString();
                String password1=et_password.getText().toString();
                Log.i("msg",password1);
                String password2=et_rePassword.getText().toString();
                Log.i("msa",password2);
                if(!password1.equals(password2)){
                    et_rePassword.setError("密码不一致");
                    return;}
                String tel=et_tel.getText().toString();
                RegiestModel moder=new RegiestModel();
                moder.regiester(userName,password1,tel,this);
                break;
            case R.id.zreturn:
                Intent intent =new Intent(context,login_Activity.class);
                break;
        }
    }

    @Override
    public void onResponse(String regInfo) {
        Log.i("msg",regInfo);
        if(regInfo.contains("1")) {
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(this,"注册失败,用户名已存在",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {

    }
}
